use([
    "public/javascripts/service/product_listService.js",
    "public/javascripts/widget/slider/slider.js",
    "public/javascripts/product_list/view.js",
    "public/javascripts/widget/notie/notie.js",
], function(product_listService, SliderComponent, jQView, NotieComponent) {
    var PageComponent = React.createClass({
        restData:{
            'q':'',
            'topGrade':0,
            'secondGrade':'',
            'pageIndex':1,
            'pageSize':10
        },
        componentDidMount: function() {
            jQView.init();
            this.getUnreadMsg();
        },
        receiveMsg: function(data) {

        },
        getInitialState: function () {
            return this.props.data ? this.props.data : {};
        },
        render:function() {
            var data = this.state.data;

            var _this = this;
            var bigtype = _this.state.bigtype ? _this.state.bigtype : [],
                smalltype = _this.state.smalltype ? _this.state.smalltype : [],
                parentid = _this.state.parentid,
                childid = _this.state.childid,
                list = _this.state.list ? _this.state.list : [],
                totalResult = _this.state.totalResult ? _this.state.totalResult : 0,
                pageIndex = _this.state.pageIndex ? _this.state.pageIndex : 1,
                Browser = typeof window != 'undefined';

            return ~init;
        },
        search: function(isReset){
            var _this = this;
            _this.restData.q = $('#search').val();

            isReset ? _this.restData.pageIndex = 1 : '';

            var topGrade = _this.state.parentid ? _this.state.parentid : 0,
                secondGrade = _this.state.childid ? _this.state.childid : '';

            var config = {
                'urlParams':{
                    'q':_this.restData.q,
                    'topGrade':topGrade,
                    'secondGrade':secondGrade,
                    'pageIndex':_this.restData.pageIndex,
                    'pageSize':_this.restData.pageSize
                }
            };

            product_listService.getList(config, function (res) {
                if(res.errorCode === 0){
                    _this.refresh(res);
                }
            });
        },
        refresh: function(res){
            var _this = this,
                data = res.data,
                arr = [];

            for(var i=0, l=data.length; i<l; i++){
                arr.push(data[i]);
            }
            _this.setState({list:arr,'totalResult':res.pagination.totalResult,'pageIndex':_this.restData.pageIndex});
        },
        enter: function(){
            var _this = this;

            if(event.keyCode != 13){
                return;
            }

            _this.search(true);
        },
        highVolumeOrders:function () {
            window.location.href='http://' + location.host+'/highVolumeOrders?&relate=product_list';
        },
        onPageChange:function(pageIndex, pageSize){
            var _this = this;

            _this.restData.pageIndex = pageIndex;
            _this.restData.pageSize = pageSize;

            _this.search();
        },
        getUnreadMsg: function() {
            var _this = this;
            product_listService.getUnreadMsg({}, function(res) {
                if (res.errorCode === 0 && res.data.unReadCount > 0) {
                    _this.refs.notie.init('新消息通知', '系统提示', '您有' + res.data.unReadCount + '条未读消息, 点击查看', 5000, './notification');
                }
            })
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});